import pygame
from pygame.locals import *
JUMP_HEIGHT = 80           # pixels
MAN_SCREEN_MARGIN = 5       # pels
JUMPING_DURATION = 700      # milliseconds
HORZ_MOVE_INCREMENT = 10     # pixels
TIME_AT_PEAK = JUMPING_DURATION / 2
FLYING_TIME = JUMPING_DURATION / 3
FALLING_SPEED = 5

class Wolf(pygame.sprite.Sprite):
	"""This class represents Wolf"""
	def __init__(self, position, world):
		pygame.sprite.Sprite.__init__(self)
		self.world = world
		self.running_frames = ["right-walking1", "right-walking2","right-walking3","right-walking4","right-walking5","right-walking6"]
		self.jumping_frames = ["jump1", "jump2","jump3"]
		self.image = pygame.image.load('wolf.png')
		self.rect = self.image.get_rect()

		self.actions = {
						  	"right-walking1": 	(0, 0, 50, 27),
						  	"right-walking2": 	(50, 0, 50, 27),
							"right-walking3": 	(100, 0, 50, 27),
						  	"right-walking4": 	(150, 0, 50, 27),
						  	"right-walking5": 	(200, 0, 50, 27),
						  	"right-walking6": 	(250, 0, 50, 27),

						  	"jump1": 	(0, 0, 50, 27),
						  	"jump2": 	(50, 0, 50, 27),
						  	"jump3": 	(100, 0, 50, 27)

					   }

		self.action = "right-walking1"
		self.area = pygame.rect.Rect(self.actions[self.action])
		self.rect.topleft = position
		self.frame=0
		self.direction = "left"
		self.jumping = False
		self.jumpingHorz = 0
		self.jump_frame = 0
		self.jump_stop_frame = 13
		self.jump_forward = 0
		self.jump_air_power = 3 #sideways power in jump
		self.jump_speed = 5
		self.fall_speed = FALLING_SPEED
		self.fall_tolerance = 3
		self.lived=True
		self.running_speed = 8
		self.floor=world.floorY(self.rect.x+50)

	def get_frame(self, frame_set):
		self.frame += 1
		if self.frame > (len(frame_set) - 1):
			self.frame = 0

		return frame_set[self.frame]

	def handle_event(self, event, floor):

		def jumpHeightAtTime(elapsedTime):
			return ((-1.0/TIME_AT_PEAK**2)* ((elapsedTime-TIME_AT_PEAK)**2)+1)*JUMP_HEIGHT
		def getframejumping(t):
			if t<FLYING_TIME:
				return self.jumping_frames[0]
			elif (t<FLYING_TIME*2):
				return self.jumping_frames[1]
			else:
				return self.jumping_frames[2]
		def isAround(i,j,tolerance):
			return i > (j - tolerance) and i < (j + tolerance)
		def check_if_falling():
			return self.floor<floor.floorY(self.rect.x+50)
		keys = pygame.key.get_pressed()
		jumpHeight = 0
		if not self.jumping:
			if keys[K_SPACE]or keys[K_UP]:
				self.jumping = True
				self.jumpingStart = pygame.time.get_ticks()
			elif keys[K_DOWN]:
				floor.slowdown()
			else:
				if check_if_falling():
					print "falling"
					self.floor+=self.fall_speed
					self.fall_speed+=1
					if isAround(self.floor,floor.floorY(self.rect.x+50),20):
						self.floor=floor.floorY(self.rect.x+50)
						self.fall_speed = FALLING_SPEED
					if self.floor>floor.floorY(self.rect.x+50) or self.floor==floor.DIED_LVL :
						print "die"
						self.lived=False
		if self.jumping:
			t = pygame.time.get_ticks() - self.jumpingStart
			self.jumping_frame=getframejumping(t)
			floorhigh=floor.floorY(self.rect.x+50)
			if (t > TIME_AT_PEAK and isAround(self.rect.y,floorhigh- self.image.get_height(),20)):
				self.floor=floor.floorY(self.rect.x+50)
				self.jumping = False
				jumpHeight = 0
			else:
				jumpHeight =jumpHeightAtTime(t)
		self.rect.y = self.floor - self.image.get_height() - jumpHeight
		if (self.rect.y+self.image.get_height()>=floor.DIED_LVL):
			print "die"
			self.lived=False
		#----------------------------------------------------- print self.rect.y

	def handle_animation(self):
		if self.lived:
			if self.jumping:
				self.area = pygame.rect.Rect(self.actions[self.jumping_frame])
			else:
				self.area = pygame.rect.Rect(self.actions[self.get_frame(self.running_frames)])
